/**
 * 函数式组件
 */
import React, {useState, useEffect} from "react";
import {Modal, Form, Input, Radio, Divider, message} from 'antd';
import axios from "axios";
import servicePath from "../config/apiConfig";
const AddStudentFun = (props)=>{
    const [studentInfoForm] = Form.useForm();
    const [isModalVisible, setIsModalVisible] = useState(props.isVisible);
    // 关闭对话框
    const closeModal = ()=>{
        setIsModalVisible(false);
        props.closeModal()
    }
    // 提交
    const onsubmit = ()=>{
        studentInfoForm.validateFields().then(value => {
            axios({
                method: 'post',
                url: servicePath.addStudent,
                data: {...value}
            }).then(res=>{
                const data = res.data
                if(data.code === 200) {
                    message.success('录入学生信息成功！',1);
                    closeModal()
                } else {
                    message.error('录入学生信息失败！',1);
                }
            }).catch(err=>{
                message.error(err,1);
            })
            // closeModal()
        }).catch(err=>{
            console.log(err)
        })
    }

    const layout = {
        labelCol: { span: 6 },
        wrapperCol: { span: 10 },
    };

    return (
        <div>
            <Modal title="录入学生信息" visible={isModalVisible} onOk={onsubmit} onCancel={closeModal}>
                <Form {...layout} form={studentInfoForm} name="add-student">
                    <Divider orientation="left">基本信息</Divider>
                    <Form.Item name="snumber" label="学号" rules={[{ required: true, message: '学号不能为空!'}]}>
                        <Input placeholder="请输入学号"/>
                    </Form.Item>
                    <Form.Item name="name" label="姓名" rules={[{ required: true, message: '姓名不能为空!' }]}>
                        <Input placeholder="请输入姓名"/>
                    </Form.Item>
                    <Form.Item name="classes" label="班级" rules={[{ required: true, message: '班级不能为空!' }]}>
                        <Input placeholder="请输入班级"/>
                    </Form.Item>
                    <Form.Item name="sex" label="性别" rules={[{ required: true, message: '性别不能为空!' }]}>
                        <Radio.Group>
                            <Radio value="男">男</Radio>
                            <Radio value="女">女</Radio>
                        </Radio.Group>
                    </Form.Item>
                    <Divider orientation="left">各科成绩</Divider>
                    <Form.Item name="cscore" label="c语言">
                        <Input placeholder="请输入c语言成绩"/>
                    </Form.Item>
                    <Form.Item name="jscore" label="java">
                        <Input placeholder="请输入java成绩"/>
                    </Form.Item>
                    <Form.Item name="dbscore" label="数据库">
                        <Input placeholder="请输入数据库成绩"/>
                    </Form.Item>
                </Form>
            </Modal>
        </div>
    )
}

export default AddStudentFun
